﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace RayD.Core
{
    /// <summary>
    /// 基于 RestSharp 封装HttpHelper
    /// </summary>
    public static class HttpHelper
    {

        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public static (bool, string) GetApi( string url)
        {
            try
            {
                string result = "";
                HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
                HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
                Stream stream = resp.GetResponseStream();
                try
                {
                    //获取内容
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream.Close();
                }

                return (true, result);
            }
            catch (Exception ex)
            {
                return  (true, ex.Message);
            }
        }

        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="dic">请求参数</param>
        /// <returns></returns>
        public static (bool, string) GetApi(string url, Dictionary<string, string> dic)
        {
            try
            {
                string result = "";
                StringBuilder builder = new StringBuilder();
                builder.Append(url);
                if (dic.Count > 0)
                {
                    builder.Append("?");
                    int i = 0;
                    foreach (var item in dic)
                    {
                        if (i > 0)
                            builder.Append("&");
                        builder.AppendFormat("{0}={1}", item.Key, item.Value);
                        i++;
                    }
                }
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
                //添加参数
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                try
                {
                    //获取内容
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream.Close();
                }

                return (true, result);
            }
            catch (Exception ex)
            {
                return (true, ex.Message);
            }
        }



        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">完整的url</param>
        /// <param name="body">post body,可以匿名或者反序列化</param>
        /// <returns></returns>
        public static (bool, string) PostApi(string url, string postData = "",string contenttype="json")
        {
            try
            {
                string result = "";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                switch (contenttype)
                {
                    case "json":
                    {
                        req.ContentType = "application/json"; 
                        break;
                    }
                    case "xml":
                    {
                        req.ContentType = "text/plain";
                        break;
                    }
                    case "text":
                    {
                        req.ContentType = "text/plain";
                        break;
                    }
                    default:
                    {
                        req.ContentType = "text/plain";
                        break;
                    }
                }
     

                #region 添加Post 参数
                byte[] data = Encoding.UTF8.GetBytes(postData);
                req.ContentLength = data.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }
                #endregion

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                return (true, result);
            }
            catch (Exception ex)
            {
                return (true, ex.Message);
            }
           
        }


        /// <summary>
        ///  Post请求
        /// </summary>
        /// <param name="url"请求地址</param>
        /// <param name="dic">参数</param>
        /// <returns></returns>
        public static (bool, string) PostApi(string url, Dictionary<string, string> dic)
        {
            try
            {
                string result = "";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                #region 添加Post 参数
                StringBuilder builder = new StringBuilder();
                int i = 0;
                foreach (var item in dic)
                {
                    if (i > 0)
                        builder.Append("&");
                    builder.AppendFormat("{0}={1}", item.Key, item.Value);
                    i++;
                }
                byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
                req.ContentLength = data.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }
                #endregion
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                return (true, result);
            }
            catch (Exception ex)
            {
                return (false, ex.Message);
            }
        }


        /// <summary>
        /// POS请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <returns></returns>
        public static (bool, string) PostApi(string url)
        {
            try
            {
                string result = "";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }

                return (true, result);
            }
            catch (Exception ex)
            {
                return (false, ex.Message);
            }
           
        }
    }
}
